package com.ptu.spzx.controller;


import com.github.pagehelper.PageInfo;
import com.ptu.spzx.domain.Class.Classes;
import com.ptu.spzx.domain.Class.dto.ClassesDTO;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.model.entity.system.SysDept;
import com.ptu.spzx.model.entity.system.SysUser;
import com.ptu.spzx.service.IClassesCourseService;
import com.ptu.spzx.model.vo.common.Result;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@RestController
@Slf4j
@RequestMapping("/sc/exam/classes")
public class ClassesController {

    @Autowired
    IClassesCourseService classesService;

    //添加班级
    @PostMapping("/addCls")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:addCls')")
    public Result addCls(@RequestBody ClassesDTO cls) {
        classesService.addClass(cls);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //更新班级
    @PutMapping("/updateClass")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:updateClass')")
    public Result updateClass(@RequestBody ClassesDTO classesDTO) {
        classesService.updateClass(classesDTO);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


//    批量删除班级
    @DeleteMapping("/deleteBatchClassByIds")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:deleteBatchClassByIds')")
    public Result deleteBatchClassByIds(@RequestBody DeleteIds deleteIds) {
        classesService.deleteBatchClassByIds(deleteIds);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    //批量获取班级
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:findByPage')")
    public Result findByPage(@PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             @RequestParam ("keyWord") String keyWord,
                             @RequestParam(required = false,value = "courseId") Long courseId){
        PageInfo<SysDept> pageInfo = classesService.findByPage(pageNum,pageSize,keyWord,courseId);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }

    //根据班级获取学生
    @GetMapping("/findAllStuByClass/{classIds}/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:findAllStuByClass')")
    public Result findAllStuByClass(@PathVariable("classIds") List<Long> classIds,
                                    @PathVariable("pageNum") Integer pageNum,
                                    @PathVariable("pageSize") Integer pageSize){
        PageInfo<SysUser> allStuByClass = classesService.findAllStuByClass(classIds, pageNum, pageSize);
        return Result.build(allStuByClass,ResultCodeEnum.SUCCESS);
    }
    //根据课程获取学生
    @GetMapping("/findAllStuByCourse/{courseIds}/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:findAllStuByCourse')")
    public Result findAllStuByCourse(@PathVariable("courseIds") List<Long> courseIds,
                                    @PathVariable("pageNum") Integer pageNum,
                                    @PathVariable("pageSize") Integer pageSize){
        PageInfo<SysUser> allStuByClass = classesService.findAllStuByCourse(courseIds, pageNum, pageSize);
        return Result.build(allStuByClass,ResultCodeEnum.SUCCESS);
    }

    //通过考试ID获取全部考试学生
    @GetMapping("/getStuListByExamId/{examId}/{pageNum}/{pageSize}")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:getStuListByExamId')")
    public Result getStuListByExamId(@PathVariable("examId") Long examId,
                                     @PathVariable("pageNum") Integer pageNum,
                                     @PathVariable("pageSize") Integer pageSize){
        PageInfo<SysUser> allStuByClass = classesService.findAllStuByExam(examId, pageNum, pageSize);
        return Result.build(allStuByClass,ResultCodeEnum.SUCCESS);
    }

    //根据课程获取班级
    @GetMapping("/getClassByCourse")
    @PreAuthorize("@permissionService.hasPermission('exam:classes:getClassByCourse')")
    public Result getClassByCourse(@RequestParam Long courseId,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageInfo<SysDept> classPageInfo= classesService.getClassByCourse(courseId,pageNum,pageSize);
        return Result.build(classPageInfo,ResultCodeEnum.SUCCESS);
    }

}

